package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.TransactionSummary;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Transactionrecords;
import com.ruoyi.system.service.ITransactionrecordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 交易记录Controller
 * 
 * @author yy
 * @date 2024-08-13
 */
@RestController
//http://localhost/system/transaction/transactionOverview
@RequestMapping("/system/transaction")
public class TransactionrecordsController extends BaseController
{
    @Autowired
    private ITransactionrecordsService transactionrecordsService;

    /**
     * 查询交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:list')")
    @GetMapping("/list")
    public TableDataInfo list(Transactionrecords transactionrecords)
    {
        List<Transactionrecords> list = transactionrecordsService.selectTransactionrecordsList(transactionrecords);
        return getDataTable(list);
    }

    /**
     * 导出交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:export')")
    @Log(title = "交易记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Transactionrecords transactionrecords)
    {
        List<Transactionrecords> list = transactionrecordsService.selectTransactionrecordsList(transactionrecords);
        ExcelUtil<Transactionrecords> util = new ExcelUtil<Transactionrecords>(Transactionrecords.class);
        util.exportExcel(response, list, "交易记录数据");
    }

    /**
     * 获取交易记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:query')")
    @GetMapping(value = "/{trid}")
    public AjaxResult getInfo(@PathVariable("trid") Long trid)
    {
        return success(transactionrecordsService.selectTransactionrecordsByTrid(trid));
    }

    /**
     * 新增交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:add')")
    @Log(title = "交易记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Transactionrecords transactionrecords)
    {
        return toAjax(transactionrecordsService.insertTransactionrecords(transactionrecords));
    }

    /**
     * 修改交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:edit')")
    @Log(title = "交易记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Transactionrecords transactionrecords)
    {
        return toAjax(transactionrecordsService.updateTransactionrecords(transactionrecords));
    }

    /**
     * 删除交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:transaction:remove')")
    @Log(title = "交易记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trids}")
    public AjaxResult remove(@PathVariable Long[] trids)
    {
        return toAjax(transactionrecordsService.deleteTransactionrecordsByTrids(trids));
    }
    //transactionOverview
    @PreAuthorize("@ss.hasPermi('system:transaction:query')")
    @Anonymous
    @GetMapping("/transactionOverview")
    public ResponseEntity<?> showTransactionOverview() {
        BigDecimal chargingAmount = transactionrecordsService.calculateChargingAmountForToday();
        int chargingCount = transactionrecordsService.calculateChargingCountForToday();
        BigDecimal rechargingAmount = transactionrecordsService.calculateRechargingAmountForToday();
        int rechargingCount = transactionrecordsService.calculateRechargingCountForToday();
        return ResponseEntity.ok(new TransactionSummary(chargingAmount, chargingCount, rechargingAmount, rechargingCount));
    }

}
